﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.BaseData;
using BOM.dao;
using BOM.entity;
using System.IO;
using NPOI.HSSF.UserModel;

namespace BOM.PriceBill
{
    public partial class PriceBillForm : Form
    {
        public PriceBillForm()
        {
            InitializeComponent();
        }
        public static int number = 0; //记下用户是否引发comboBox值得变化
        public static string fname = "null";//记下用户在comboBox中选取的值
        public int k = 0; //控制当datagridview的单元格值发生变化时发生循环的次数
        public float MaterialSum = 0;//计算材料的合计成本
        public static string ExcelString = "";
        public bool boool = false;
        public static int ViewLong = 0; //记下datagridview里面数据的长度
        public static bool btnsenve = false; //判断用户是否单击了 保存按钮
        public static int Onefid = 0;
        public static int RowIndex = 0;//记下当前选中行的
        public static float Area = 0;//记录下当前的表面积
        public static float Perimeter = 0;//记录下当前的周长

        //加载界面comboBox组合框
        private void LoadingCutomerNumber()
        { 
            CustomerNew customer = new CustomerNew();
            DataTable table = customer.CustomerListChaKan();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];
                string str = row["fname"].ToString();
                cbCustomer.Items.Add(str);
            }
        }
        //加载datagridview控件
        private void LoadingMaterialAndMaterialgroup()
        {
            dataGridView1.AutoGenerateColumns = false;
            DataTable table = PriceBillNew.SelectMaterialgroupAndMaterial();
            if (table != null)
            {
                dataGridView1.DataSource = table;
                
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells["qty"].Value = 0;
                    dataGridView1.Rows[i].Cells["amount"].Value = 0;
                }
                boool = true;
            }
        }
        //重新加载datagridview的数据
        public void NewLoadingdatagridview()
        {
            DataTable table1 = PriceBillNew.SelectPriceBillEntryAndMaterial(CustomerList.XuanZID);
            if (table1 != null)
            {
                dataGridView1.DataSource = table1;
                ViewLong = dataGridView1.Rows.Count;
            }
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells["isDetail"].Value ==null 
                    || Boolean.Parse(dataGridView1.Rows[i].Cells["isDetail"].Value.ToString())== false)
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                else
                    dataGridView1.Rows[i].Cells["materialGroup"].Value = null;
            }
        }
        //为datagridview减少一行
        public void ReduceRow()
        {
            this.dataGridView1.RowCount--; 
        }
        //保存按钮
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cbCustomer.Text.Trim() == "")
            {
                MessageBox.Show("客户是必填的", "提示");
            }
            else if (txtDifficulty.Text.Trim() == "")
            {
                MessageBox.Show("难度系数是必须的","提示");
            }
            else
            {
                try
                {
                    int fcustomerid = -1;
                    PriceBillsurface priceBill = new PriceBillsurface();
                    priceBill.Fnumber = txtNumber.Text;
                    priceBill.Fname = txtName.Text;
                    DataTable table = PriceBillNew.SelectFnameFid(cbCustomer.Text);
                    if (table != null)
                    {
                        fcustomerid = Convert.ToInt32(table.Rows[0]["fid"]);
                        priceBill.Fcustomerid = fcustomerid;
                    }
                    else
                    {
                        priceBill.Fcustomerid = fcustomerid;
                    }
                    priceBill.Fcreatetime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                    priceBill.Fmodel = txtModel.Text;
                    string Size = txtSize.Text.Trim();
                    string[] sArray = Size.Split('*');
                    if (sArray.Length == 3)
                    {
                        priceBill.Fsize = txtSize.Text;
                    }
                    priceBill.Funit = txtUnit.Text;
                    if (txtArea.Text != "")
                    {
                        priceBill.Farea = txtArea.Text;
                    }
                    else
                    {
                        priceBill.Farea = "0";
                    }
                    if (txtPerimeter.Text.Trim() != "")
                    {
                        priceBill.Fperimeter = txtPerimeter.Text;
                    }
                    else
                    {
                        priceBill.Fperimeter = "0";
                    }
                    if (txtDifficulty.Text.Trim() == "")
                    {
                        txtDifficulty.Text = "0";
                    }
                    priceBill.Fdifficulty = txtDifficulty.Text;
                    priceBill.FmanageFee = txtManageFee.Text;
                    priceBill.FmadeFee = txtMadeFee.Text;
                    priceBill.FmaterialFee = txtMaterialFee.Text;

                    //priceBill.FmanageFee = float.Parse(priceBill.FmanageFee.ToString("0.00"));
                    //priceBill.FmadeFee = float.Parse(priceBill.FmadeFee.ToString("0.00"));
                    //priceBill.FmaterialFee = float.Parse(priceBill.FmaterialFee.ToString("0.00"));
                    if (txtTotalCost.Text.Trim() == "")
                    {
                        txtTotalCost.Text = "0";
                    }
                    priceBill.FtotalCost = txtTotalCost.Text;
                    //priceBill.FtotalCost = float.Parse(priceBill.FtotalCost.ToString("0.00"));
                    priceBill.Fface = txtFace.Text;
                    if (CustomerList.BiaoJi == 2)
                    {
                        //最后向编码规则表中插入数据
                        bool bol = PriceBillNew.InsertPriceBill();//是向表priceBill新增数据
                        if (bol == true)
                        {
                            InsertPiceBillEntry(); //向表priceBillEntry新增数据
                            PriceBillList Pllentry = (PriceBillList)this.Owner;
                            Pllentry.LoadingDataGridView1();
                            //向表Coderule中写入数据
                            btnsenve = true;
                            //新增成功后 把标记改为修改状态
                            CustomerList.BiaoJi = 1;
                            //取刚才插入的ID 赋值给 选择的ID
                            CustomerList.XuanZID = PriceBillNew.SelectPriceBillFnuberID(txtNumber.Text);
                            MessageBox.Show("新增成功", "提示");
                            //提示新增成功
                        }
                        else
                        {
                            MessageBox.Show("新增失败", "提示");
                            //提示新增失败
                        }
                    }
                    else if (CustomerList.BiaoJi == 1)
                    {
                        bool bol = PriceBillNew.UpdatePriceBill(CustomerList.XuanZID);
                        if (bol == true)
                        {
                            PriceBillNew.DeletePriceBillEntry(CustomerList.XuanZID);
                            InsertPiceBillEntry();
                            MessageBox.Show("修改成功", "提示");
                            PriceBillList priceBilllist = (PriceBillList)this.Owner;
                            priceBilllist.LoadingDataGridView1();
                        }
                        else
                        {
                            MessageBox.Show("修改失败", "提示");
                        }
                    }
                }
                catch
                {
                    //MessageBox.Show("请填入该计价单必须的数据","提示");
                }
            }
        }
        //把数据表datagridview中记下新增到数据库当中
        public void InsertPiceBillEntry()
        {
            //int i = dataGridView1.Rows.Count;
            DataTable table = PriceBillNew.SelectFnumberPriceBill(txtNumber.Text);
            if (table != null)
            {
                int priceBillfid = Convert.ToInt32(table.Rows[0][0].ToString());
                //MessageBox.Show(priceBillfid.ToString());
                for (int i = 0, j = 0; i < dataGridView1.Rows.Count; i++)
                {
                    //if (dataGridView1.Rows[i].Cells["qty"].Value.ToString() != "0")
                    {
                        PriceBillEntry priceBill = new PriceBillEntry();
                        priceBill.Fparentid = priceBillfid;
                        if (dataGridView1.Rows[i].Cells["materialGroupId"].Value == null
                            || dataGridView1.Rows[i].Cells["materialGroupId"].Value.ToString().Trim() == "")
                            priceBill.FmaterialGroupId = -1;
                        else
                        priceBill.FmaterialGroupId = (int)dataGridView1.Rows[i].Cells["materialGroupId"].Value;
                        if (dataGridView1.Rows[i].Cells["materialId"].Value == null
                            || dataGridView1.Rows[i].Cells["materialId"].Value.ToString().Trim() == "")
                            priceBill.Fmaterialid = -1;
                        else
                        priceBill.Fmaterialid = (int)dataGridView1.Rows[i].Cells["materialId"].Value;

                        priceBill.Fsize = dataGridView1.Rows[i].Cells["size"].Value.ToString();
                        priceBill.Funit = dataGridView1.Rows[i].Cells["unit"].Value.ToString();
                        if (dataGridView1.Rows[i].Cells["price"].Value == null
                            || dataGridView1.Rows[i].Cells["price"].Value.ToString().Trim()=="" )
                            priceBill.Fprice = 0;
                        else
                            priceBill.Fprice = float.Parse(dataGridView1.Rows[i].Cells["price"].Value.ToString());
                        if (dataGridView1.Rows[i].Cells["qty"].Value == null
                            || dataGridView1.Rows[i].Cells["qty"].Value.ToString().Trim() == "")
                        {
                            //dataGridView1.Rows[i].Cells["qty"].Value = "0";
                            priceBill.Fqty = 0;
                        }   
                        else
                            priceBill.Fqty = float.Parse(dataGridView1.Rows[i].Cells["qty"].Value.ToString());

                        if (dataGridView1.Rows[i].Cells["amount"].Value == null
                            || dataGridView1.Rows[i].Cells["amount"].Value.ToString().Trim() == "")
                        {
                            // dataGridView1.Rows[i].Cells["amount"].Value = "0";
                            priceBill.Famount = 0;
                        }
                        else
                        {
                            priceBill.Famount = float.Parse(dataGridView1.Rows[i].Cells["amount"].Value.ToString());
                            priceBill.Famount = float.Parse(priceBill.Famount.ToString("0.00"));
                        }
                        //dataGridView1.AutoGenerateColumns = false;  
                        if (dataGridView1.Rows[i].Cells["description"].Value == null)
                        {
                            dataGridView1.Rows[i].Cells["description"].Value = "";
                        }
                        else
                        priceBill.Fdescription = dataGridView1.Rows[i].Cells["description"].Value.ToString();

                        priceBill.Fseq = Int32.Parse(dataGridView1.Rows[i].Cells["seq"].Value.ToString());
                        priceBill.FparentSeq = Int32.Parse(dataGridView1.Rows[i].Cells["parentSeq"].Value.ToString());
                        priceBill.FisDetail = Boolean.Parse(dataGridView1.Rows[i].Cells["isDetail"].Value.ToString());
                        PriceBillNew.PriceBillEntryInsert();//插入到数据库
                    }
                }
            }
        }
        //窗体的加载事件
        private void PriceBillForm_Load(object sender, EventArgs e)
        {
            btnsenve = false;
            Area = 0;//设定表面积变量为 0
            Perimeter = 0;//设定周长为 0
            //设定列对齐方式
            dataGridView1.Columns["price"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["amount"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridView1.Columns["qty"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            
            //设定列的小数位数
            dataGridView1.Columns["price"].DefaultCellStyle.Format = "0.00";
            dataGridView1.Columns["qty"].DefaultCellStyle.Format = "0.0000";
            dataGridView1.Columns["amount"].DefaultCellStyle.Format = "0.00";

            dataGridView1.AutoGenerateColumns = false;//设定dataGridView1列为固定的列
            btnsenve = false;
            if (CustomerList.BiaoJi == 0 || CustomerList.BiaoJi == 1)
            {
                DataTable table = PriceBillNew.SelectReasonFid(CustomerList.XuanZID);
                if (table.Rows.Count == 1)
                {
                    PriceBillsurface priceBill = new PriceBillsurface();
                    priceBill.Fnumber = table.Rows[0]["fnumber"].ToString();
                    priceBill.Fname=table.Rows[0]["fname"].ToString();
                    //priceBill.Fcustomerid = Convert.ToInt32(table.Rows[0]["fcustomerid"]);
                    priceBill.Fmodel = table.Rows[0]["fmodel"].ToString();
                    priceBill.Fsize = table.Rows[0]["fsize"].ToString();
                    priceBill.Funit = table.Rows[0]["funit"].ToString();
                    priceBill.Farea = float.Parse(table.Rows[0]["farea"].ToString()).ToString("0.0000");
                    priceBill.Fperimeter = float.Parse(table.Rows[0]["fperimeter"].ToString()).ToString("0.00");
                    priceBill.Fdifficulty = float.Parse(table.Rows[0]["fdifficulty"].ToString()).ToString("0.00");
                    priceBill.FmanageFee = float.Parse(table.Rows[0]["fmanageFee"].ToString()).ToString("0.00");
                    priceBill.FmadeFee = float.Parse(table.Rows[0]["fmadeFee"].ToString()).ToString("0.00");
                    priceBill.FmaterialFee = float.Parse(table.Rows[0]["fmaterialFee"].ToString()).ToString("0.00");
                    priceBill.FtotalCost = float.Parse(table.Rows[0]["ftotalCost"].ToString()).ToString("0.00");
                    priceBill.Fface = table.Rows[0]["fface"].ToString();
                }
                ViewLong = dataGridView1.Rows.Count;
            }
            //上级窗口点击了新增
            if (CustomerList.BiaoJi == 2)
            {

                txtName.Text = "松誉音响设备有限公司计价表";
                cbCustomer.Text = "";
                txtModel.Text = "";
                txtSize.Text = "";
                txtUnit.Text = "";
                txtArea.Text = "";
                txtPerimeter.Text = "";
                txtDifficulty.Text = "";
                txtManageFee.Text = "";
                txtMadeFee.Text = "";
                txtMaterialFee.Text = "";
                txtTotalCost.Text = "";
                if (CodeRuleNew.SelectCodeRule() == false)
                {
                    CodeRule coderule = new CodeRule();
                    coderule.Fname = "计价单编码规则";
                    coderule.Findex = 1;
                    CodeRuleNew.InsertCodeRule();
                    int fid = Convert.ToInt32(CodeRuleNew.GetFid().Rows[0]["fid"]);
                    coderule.Fname = "计价单编码规则";
                    coderule.Findex = fid;
                    CodeRuleNew.UpdateCodeRule(fid);
                }
                else
                {
                    CodeRule coderule = new CodeRule();
                    coderule.Fname = "计价单编码规则";
                    CodeRuleNew.InsertCodeRule();
                    int fid = Convert.ToInt32(CodeRuleNew.GetFid().Rows[0]["fid"]);
                    coderule.Fname = "计价单编码规则";
                    coderule.Findex = fid;
                    CodeRuleNew.UpdateCodeRule(fid);
                }
                int ffid = Convert.ToInt32(CodeRuleNew.GetFid().Rows[0]["fid"]); //得到刚才插入主键的值
                int fidex = Convert.ToInt32(CodeRuleNew.SelectFidCodeRule(ffid).Rows[0]["fid"]); //根据主键的到编码序列号
                if (fidex.ToString().Length == 1)
                {
                    txtNumber.Text = "" + DateTime.Now.ToString("yyyyMMdd").ToString() + "00" + fidex.ToString() + "";
                }
                else if (fidex.ToString().Length == 2)
                {
                    txtNumber.Text = "" + DateTime.Now.ToString("yyyyMMdd").ToString() + "0" + fidex.ToString() + "";
                }
                else
                {
                    txtNumber.Text = "" + DateTime.Now.ToString("yyyyMMdd").ToString() + "" + fidex.ToString() + "";
                }
                //LoadingMaterialAndMaterialgroup();//加载datagridview控件
                LoadingCutomerNumber(); //加载comboBox
                btnRemove.Enabled = false;
            }
            //上级窗口点击了编辑
            else if (CustomerList.BiaoJi == 1)
            {
                DataTable table = PriceBillNew.SelectReasonFid(CustomerList.XuanZID);
                PriceBillsurface priceBill = new PriceBillsurface();
                txtNumber.Text = priceBill.Fnumber;
                txtName.Text = priceBill.Fname;
                string fname = PriceBillNew.SelectFidFname(Convert.ToInt32(table.Rows[0]["fcustomerid"]));
                cbCustomer.Text = fname;
                txtModel.Text = priceBill.Fmodel;
                txtSize.Text = priceBill.Fsize;
                txtUnit.Text = priceBill.Funit;
                txtArea.Text = priceBill.Farea.ToString();
                txtPerimeter.Text = priceBill.Fperimeter.ToString();
                txtDifficulty.Text = priceBill.Fdifficulty.ToString();
                txtManageFee.Text = priceBill.FmanageFee.ToString();
                txtMadeFee.Text = priceBill.FmadeFee.ToString();
                txtMaterialFee.Text = priceBill.FmaterialFee.ToString();
                txtTotalCost.Text = priceBill.FtotalCost.ToString();
                txtFace.Text = priceBill.Fface.ToString();
                txtFace.ReadOnly = false;
                txtNumber.ReadOnly = true;
                txtName.ReadOnly = false;
                //cbCustomer.ReadOnly = false;
                txtModel.ReadOnly = false;
                txtSize.ReadOnly = false;
                txtUnit.ReadOnly = false;
                txtDifficulty.ReadOnly = false;
                txtManageFee.ReadOnly = false;
                txtMadeFee.ReadOnly = false;
                txtTotalCost.ReadOnly = false;
                btnAddLine.Enabled = false;
                btnEditLine.Enabled = false;
                btnDelLine.Enabled = false;
                NewLoadingdatagridview();//加载datagridview数据
                LoadingCutomerNumber();//加载combox

                //修改的代码
            }
            //上级窗口点击了查看
            else if (CustomerList.BiaoJi == 0)
            {
                DataTable table = PriceBillNew.SelectReasonFid(CustomerList.XuanZID);
                PriceBillsurface priceBill = new PriceBillsurface();
                txtNumber.Text = priceBill.Fnumber;
                txtName.Text = priceBill.Fname;
                string fname1 = PriceBillNew.SelectFidFname(Convert.ToInt32(table.Rows[0]["fcustomerid"]));
                cbCustomer.Text = fname1;
                txtModel.Text = priceBill.Fmodel;
                txtSize.Text = priceBill.Fsize;
                txtUnit.Text = priceBill.Funit;
                txtArea.Text = priceBill.Farea.ToString();
                txtPerimeter.Text = priceBill.Fperimeter.ToString();
                txtDifficulty.Text = priceBill.Fdifficulty.ToString();
                txtManageFee.Text = priceBill.FmanageFee.ToString();
                txtMadeFee.Text = priceBill.FmadeFee.ToString();
                txtMaterialFee.Text = priceBill.FmaterialFee.ToString();
                txtTotalCost.Text = priceBill.FtotalCost.ToString();
                txtFace.Text = priceBill.Fface.ToString();
                txtFace.ReadOnly = true;
                txtNumber.ReadOnly = true;
                txtName.ReadOnly = true;
                txtModel.ReadOnly = true;
                txtSize.ReadOnly = true;
                txtUnit.ReadOnly = true;
                txtDifficulty.ReadOnly = true;
                txtManageFee.ReadOnly = true;
                txtMadeFee.ReadOnly = true;
                txtTotalCost.ReadOnly = true;
                btnEdit.Enabled = true;
                btnRemove.Enabled = false;
                btnSave.Enabled = false;
                btnAddLine.Enabled = false;
                btnEditLine.Enabled = false;
                btnDelLine.Enabled = false;
                NewLoadingdatagridview();//加载datagridview的数据
            }
        }
        //comboBox值发生变化所引发的事件
        private void cbCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (CustomerList.BiaoJi == 2)
            //{
            //    Customer customer = new Customer();
            //    number = 1;
            //    fname = cbCustomer.Text;
            //    if (fname.Trim() != "")
            //    {
            //        customer.ShowDialog();
            //    }
            //}
        }
        //当datagridview的单元格值发生变化时发生
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //if (CustomerList.BiaoJi == 2 || CustomerList.BiaoJi == 1)
            //{
            //    //实现datagridview的联动变化
            //    int i = (int)e.RowIndex;
            //    if (k == 0)
            //    {
            //        if (i != -1 || boool == true)
            //        {
            //            k++;
            //            float price = float.Parse(dataGridView1.Rows[i].Cells["price"].Value.ToString());
            //            float qty = float.Parse(dataGridView1.Rows[i].Cells["qty"].Value.ToString());
            //            //MessageBox.Show(qty.ToString());
            //            float sum = price * qty;
            //            dataGridView1.Rows[i].Cells["amount"].Value = sum;
            //        }
            //    }
            //    k = 0;
            //}
        }
        //离开控件范围 就计算材料费合计
        private void dataGridView1_MouseLeave(object sender, EventArgs e)
        {
            try
            {
                if (CustomerList.BiaoJi == 2 || CustomerList.BiaoJi == 1)
                {
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if(Boolean.Parse(dataGridView1.Rows[i].Cells["isDetail"].Value.ToString())==false)
                        MaterialSum += float.Parse(dataGridView1.Rows[i].Cells["amount"].Value.ToString());
                    }
                    txtMaterialFee.Text = MaterialSum.ToString("0.00");
                }
            }
            catch(Exception e8)
            {
                MessageBox.Show(e8.Message);
            }
        }
        //鼠标进入控件 把Sum赋值为0
        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
        {
            if (CustomerList.BiaoJi == 2 || CustomerList.BiaoJi == 1)
            {
                MaterialSum = 0;
            }
        }
        private void txtArea_Validated(object sender, EventArgs e)
        {

        }
        //当控件成功输入后发生 （计算面积和周长）
        private void txtSize_Validated(object sender, EventArgs e)
        {
            
               string Size = txtSize.Text.Trim();
                if (Size != "")
                {
                    try
                    {
                        string forever = ""; //长
                        string wide = ""; //宽
                        string tall = ""; //高
                        string[] sArray = Size.Split('*');
                        forever = sArray[0].Trim();
                        wide = sArray[1].Trim();
                        tall = sArray[2].Trim();
                        float Perimeter;//周长
                        float Areaa;//表面积
                       // Areaa = (float.Parse(forever) * float.Parse(wide) + float.Parse(forever) * float.Parse(tall) + float.Parse(tall) * float.Parse(wide)) * 2;
                        Perimeter = (float.Parse(forever) + float.Parse(wide) + float.Parse(tall)) * 4 * 2/1000;
                        Perimeter = float.Parse(Perimeter.ToString("0.00"));
                        //txtArea.Text = Areaa.ToString();
                    
                        txtPerimeter.Text = Perimeter.ToString();
                    }
                    catch
                    {
                        MessageBox.Show("规格字段格式不正确，请按长*宽*高输入", "提示");
                    }
                }
            
        }
        //管理成本输入成功后发生
        private void txtManageFee_Validated(object sender, EventArgs e)
        {
            if (txtManageFee.Text.Trim() == "")
            {
                txtManageFee.Text = "0";
            }
            if (txtMadeFee.Text.Trim() == "")
            {
                txtMadeFee.Text = "0";
            }
            if (txtMaterialFee.Text.Trim() == "")
            {
                txtMaterialFee.Text = "0";
            }
            txtTotalCost.Text = (float.Parse(txtManageFee.Text.Trim()) + float.Parse(txtMadeFee.Text.Trim()) + float.Parse(txtMaterialFee.Text.Trim())).ToString();
        }
        //制造成本输入成功后发生
        private void txtMadeFee_Validated(object sender, EventArgs e)
        {
            if (txtManageFee.Text.Trim() == "")
            {
                txtManageFee.Text = "0";
            }
            if (txtMadeFee.Text.Trim() == "")
            {
                txtMadeFee.Text = "0";
            }
            if (txtMaterialFee.Text.Trim() == "")
            {
                txtMaterialFee.Text = "0";
            }
            txtTotalCost.Text = (float.Parse(txtManageFee.Text.Trim()) + float.Parse(txtMadeFee.Text.Trim()) + float.Parse(txtMaterialFee.Text.Trim())).ToString();
        }
        //材料费用文本框值发生变化的事件
        private void txtMaterialFee_TextChanged(object sender, EventArgs e)
        {
            if (txtManageFee.Text.Trim() == "")
            {
                txtManageFee.Text = "0";
            }
            if (txtMadeFee.Text.Trim() == "")
            {
                txtMadeFee.Text = "0";
            }
            if (txtMaterialFee.Text.Trim() == "")
            {
                txtMaterialFee.Text = "0";
            }
            txtTotalCost.Text = (float.Parse(txtManageFee.Text.Trim()) + float.Parse(txtMadeFee.Text.Trim()) + float.Parse(txtMaterialFee.Text.Trim())).ToString();
        }
        //编辑按钮
        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnEditLine.Enabled = true;
            btnDelLine.Enabled = true;
            txtNumber.ReadOnly = true;
            txtName.ReadOnly = false;
            txtModel.ReadOnly = false;
            txtSize.ReadOnly = false;
            txtUnit.ReadOnly = false;
            txtDifficulty.ReadOnly = false;
            txtManageFee.ReadOnly = false;
            txtMadeFee.ReadOnly = false;
            btnSave.Enabled = true;
            btnRemove.Enabled = true;
            CustomerList.BiaoJi = 1;
            LoadingCutomerNumber();//加载combox
        }
        //删除按钮
        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("您确定要删除吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    bool bol = PriceBillNew.DeleteFidPriceBill(CustomerList.XuanZID);
                    if (bol == true)
                    {
                        bool bl = PriceBillNew.DeletePriceBillEntry(CustomerList.XuanZID);
                        if (bl == true)
                        {
                            PriceBillList priceBilllist = (PriceBillList)this.Owner;
                            priceBilllist.LoadingDataGridView1();
                            MessageBox.Show("删除成功", "提示");
                            txtNumber.Text = "";
                            txtName.Text = "";
                            cbCustomer.Text = "";
                            txtModel.Text = "";
                            txtSize.Text = "";
                            txtUnit.Text = "";
                            txtArea.Text = "";
                            txtPerimeter.Text = "";
                            txtDifficulty.Text = "";
                            txtManageFee.Text = "";
                            txtMadeFee.Text = "";
                            txtMaterialFee.Text = "";
                            txtTotalCost.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("删除失败", "提示");
                        }
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示");
                    }
                }
            }
            catch
            { }
        }
        //退出按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //导出为excel按钮
        private void DeriveExcel_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "文本文件|*.txt|Excel文件|*.xls|所有文件|*.*";
                saveFileDialog.FilterIndex = 2;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.FileName = "" + txtName.Text ;
                string DataExcel = GetData();
                //MessageBox.Show(DataExcel);测试OK
                string[] LineArray = DataExcel.Split('!');
                //打开这个模板
                String filepath = "Resources/ExpTemplate.xls";
                FileStream file = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                HSSFWorkbook workbook = new HSSFWorkbook(file);
                
                HSSFSheet sheet = workbook.GetSheet("Tabelle1");
                //HSSFWorkbook workbook = new HSSFWorkbook();
                //HSSFSheet sheet = workbook.CreateSheet("new　sheet");

                //向这个模板写入第一行
                HSSFRow row = sheet.GetRow(0);
                HSSFCell ExcelCell = row.GetCell(0);
                ExcelCell.SetCellValue(LineArray[0].ToString());

                //向这个模板写入第2.3行
                for (int i = 1; i < 3; i++)
                {
                    row = sheet.GetRow(i);
                    int kk = 1;//记录写入的是多少的行！
                    string[] ListArray = LineArray[i].Split('|');
                    foreach (string str in ListArray)
                    {
                        ExcelCell = row.GetCell(kk);
                        ExcelCell.SetCellValue(str);
                        kk += 7;
                    }
                }
                //写入第四行
                row = sheet.GetRow(3);
                string[] List = LineArray[3].Split('|');
                int hang = 1;
                for (int i = 0; i < List.Length; i++)
                {
                    string str = List[i].ToString();
                    ExcelCell = row.GetCell(hang);
                    ExcelCell.SetCellValue(str);
                    if (i == 1)
                    {
                        hang = 11;
                    }
                    else if (i == 2)
                    {
                        hang = 15;
                    }
                    else if (i == 3)
                    {
                        hang = 20;
                    }
                    else
                    {
                        hang = 8;
                    }
                }


                //写入表单的几行
                for (int i = 5; i < LineArray.Length - 1; i++)
                {
                    row = sheet.GetRow(i);
                    int kk = 0;//记录写入的是多少的行！
                    string[] ListArray = LineArray[i - 1].Split('|');
                    for (int j = 0; j < ListArray.Length; j++)
                    {
                        string str = ListArray[j].ToString();
                        ExcelCell = row.GetCell(kk);

                        HSSFCellStyle styleee = workbook.CreateCellStyle();
                        if(j==3 || j== 4 || j==5)
                        styleee.Alignment = HSSFCellStyle.ALIGN_RIGHT;
                        else if(j==0)
                            styleee.Alignment = HSSFCellStyle.ALIGN_LEFT;
                        else
                            styleee.Alignment = HSSFCellStyle.ALIGN_CENTER;
                        HSSFFont fontee = workbook.CreateFont();
                        if (ListArray.Length == 1)
                        {
                            fontee.FontHeightInPoints = 10;//设置字体
                            fontee.FontName = "Arial Black";
                            styleee.SetFont(fontee);
                        }
                        else
                        {
                            fontee.FontHeightInPoints = 11;//设置字体
                            fontee.FontName = "Arial";
                            styleee.SetFont(fontee);
                        }
                        ExcelCell.CellStyle = styleee;

                        ExcelCell.SetCellValue(str);
                        if (j == 0)
                        {
                            kk = 5;
                        }
                        else if (j == 1)
                        {
                            kk = 9;
                        }
                        else if (j == 2)
                        {
                            kk = 11;
                        }
                        else if (j == 3)
                        {
                            kk = 14;
                        }
                        else if (j == 4)
                        {
                            kk = 17;
                        }
                        else if (j == 5)
                        {
                            kk = 20;
                        }
                    }
                }

                //写入的末2的一行
                row = sheet.GetRow(LineArray.Length - 1);
                sheet.AddMergedRegion(new NPOI.HSSF.Util.CellRangeAddress(LineArray.Length - 1, LineArray.Length - 1, 0, 21));
                ExcelCell = row.GetCell(0);

                HSSFCellStyle style = workbook.CreateCellStyle();
                HSSFFont font = workbook.CreateFont();
                font.FontHeightInPoints = 12;//设置字体
                font.FontName = "Arial Black";
                style.SetFont(font);

                ExcelCell.CellStyle = style;

                ExcelCell.SetCellValue(LineArray[LineArray.Length - 2].ToString());

                //写入最后一行
                row = sheet.GetRow(LineArray.Length);
                sheet.AddMergedRegion(new NPOI.HSSF.Util.CellRangeAddress(LineArray.Length, LineArray.Length, 0, 21));
                ExcelCell = row.GetCell(0);

                font.FontHeightInPoints = 12;//设置字体
                font.FontName = "Arial Black";
                style.SetFont(font);
                ExcelCell.CellStyle = style;

                ExcelCell.SetCellValue("" + LineArray[LineArray.Length - 1].ToString() + "");
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string fName = saveFileDialog.FileName;
                    using (FileStream stream = new FileStream("" + fName + "", FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        workbook.Write(stream);
                    }
                    MessageBox.Show("保存计价单成功", "提示");
                }
            }
            catch(Exception e4)
            {
                MessageBox.Show(e4.Message);
            }
        }
        

        //关闭窗体时发生
        private void PriceBillForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (btnsenve == false)
                { //当保存不成功时 删除添加的 计价单编码规则 
                    int fid = Convert.ToInt32(CodeRuleNew.GetFid().Rows[0]["fid"]);
                    CodeRuleNew.DeleteCodeRule(fid);
                }
                btnsenve = true;
            }
            catch
            { }
        }
        //新建行按钮
        private void btnAddLine_Click(object sender, EventArgs e)
        {
            PriceBillEntryForm form = new PriceBillEntryForm();
            PriceBillEntryForm. EditOrNew = false;
            form.Owner = this;
            form.ShowDialog();
        }
        public DataGridView getDataGridView()
        {
            return dataGridView1;
        }
        //编辑行按钮
        private void btnEditLine_Click(object sender, EventArgs e)
        {
            Onefid = dataGridView1.CurrentCell.RowIndex;
            if (Boolean.Parse(dataGridView1.Rows[Onefid].Cells["isDetail"].Value.ToString()) == false)
            {
                MessageBox.Show(this,"分组行不能修改,若要修改分组行，请删除分组下级行项目再删除分组重新添加","提示");
                return;
            }
            TemporaryValue value = new TemporaryValue();
            value.Fid = Int32.Parse(dataGridView1.Rows[Onefid].Cells["ID"].Value.ToString());           
            value.Fmaterialgroup = dataGridView1.Rows[Onefid].Cells["materialGroup"].Value.ToString();
            value.FmaterialGroupId = Int32.Parse(dataGridView1.Rows[Onefid].Cells["materialGroupId"].Value.ToString());
            value.Fmaterial = dataGridView1.Rows[Onefid].Cells["material"].Value.ToString();
            value.Fsize = dataGridView1.Rows[Onefid].Cells["size"].Value.ToString();
            value.Funit = dataGridView1.Rows[Onefid].Cells["unit"].Value.ToString();
            value.Fpric = float.Parse(dataGridView1.Rows[Onefid].Cells["price"].Value.ToString());
            value.Fqty = float.Parse(dataGridView1.Rows[Onefid].Cells["qty"].Value.ToString());
            value.Famout = float.Parse(dataGridView1.Rows[Onefid].Cells["amount"].Value.ToString());
            value.Fdescription = dataGridView1.Rows[Onefid].Cells["description"].Value.ToString();
            value.Fseq = Int32.Parse(dataGridView1.Rows[Onefid].Cells["seq"].Value.ToString());
            value.FparentSeq = Int32.Parse(dataGridView1.Rows[Onefid].Cells["parentSeq"].Value.ToString());
            value.FisDetail = Boolean.Parse(dataGridView1.Rows[Onefid].Cells["isDetail"].Value.ToString());
            PriceBillEntryForm form = new PriceBillEntryForm();
            form.Owner = this;
            PriceBillEntryForm.EditOrNew = true;
            form.ShowDialog();
        }
        //删除行按钮
        private void btnDelLine_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("您确定要删除吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                int rowIndex = dataGridView1.CurrentCell.RowIndex;
                dataGridView1.Rows.RemoveAt(rowIndex);
                this.setSumFieldValue();
                /*
                try
                {
                    DataTable tablefid = PriceBillEntryFormNew.SelectMateriGroupsFid(dataGridView1.Rows[RowIndex].Cells["qty"].Value.ToString(), dataGridView1.Rows[RowIndex].Cells["price"].Value.ToString(), CustomerList.XuanZID);
                    int fid = Convert.ToInt32(tablefid.Rows[0]["pricebillentry.fid"].ToString());
                    bool bol = PriceBillNew.DeletePriceBillEntryFid(fid);
                    if (bol == true)
                    {
                        NewLoadingdatagridview();
                        MessageBox.Show("删除成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示");
                    }
                    //MessageBox.Show(fid.ToString());
                }
                catch
                { }
                 */
            }
        }
        public void setSumFieldValue()
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
               Object obj =  dataGridView1.Rows[i].Cells["isDetail"].Value;
               if (obj == null || Boolean.Parse(obj.ToString()) == false)
               {
                   int seq = Int32.Parse(dataGridView1.Rows[i].Cells["seq"].Value.ToString());
                   double sum = 0;
                   for (int j = 0; j < dataGridView1.Rows.Count; j++)
                   {
                       if (seq == Int32.Parse(dataGridView1.Rows[j].Cells["parentSeq"].Value.ToString()))
                       {
                           sum = sum +double.Parse(dataGridView1.Rows[j].Cells["amount"].Value.ToString());
                       }
                   }
                   dataGridView1.Rows[i].Cells["amount"].Value = sum;
               }
            }
            float totalAmount = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (Boolean.Parse(dataGridView1.Rows[i].Cells["isDetail"].Value.ToString()) == false)
                    totalAmount += float.Parse(dataGridView1.Rows[i].Cells["amount"].Value.ToString());
            }
            txtMaterialFee.Text = totalAmount.ToString("0.00");
        }
        //在窗体中传递datagridview的值
        public void Transmitdatagview(int rows)
        {
            //MessageBox.Show("成功");
            int curSeq = dataGridView1.Rows.Count;
            int parentSeq = 0;
            TemporaryValue value = new TemporaryValue();
            int insertIndex = -1;
            String materialGroup = value.Fmaterialgroup;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (materialGroup.Equals(dataGridView1.Rows[i].Cells["materialGroup"].Value))
                {
                    insertIndex = i;
                }
            }
            if (insertIndex == -1)
            {
               // DataRow dr = ((DataTable)dataGridView1.DataSource).NewRow();
                //((DataTable)dataGridView1.DataSource).Rows.Add(dr);
                int rowIndex = dataGridView1.Rows.Add();     //this.dataGridView1.Rows.Count-1;
                curSeq++;
                dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = Color.Yellow;
                dataGridView1.Rows[rowIndex].Cells["id"].Value = 0;
                dataGridView1.Rows[rowIndex].Cells["materialGroupId"].Value = value.FmaterialGroupId;
                dataGridView1.Rows[rowIndex].Cells["materialId"].Value = -1; //null;
                dataGridView1.Rows[rowIndex].Cells["materialGroup"].Value = value.Fmaterialgroup;
                dataGridView1.Rows[rowIndex].Cells["material"].Value = "";
                dataGridView1.Rows[rowIndex].Cells["size"].Value = "";
                dataGridView1.Rows[rowIndex].Cells["unit"].Value = "";
                //修改 数量和单价为空
                dataGridView1.Rows[rowIndex].Cells["price"].Value = "";//null;
                dataGridView1.Rows[rowIndex].Cells["qty"].Value = "";//null;

                dataGridView1.Rows[rowIndex].Cells["amount"].Value = 0;
                dataGridView1.Rows[rowIndex].Cells["description"].Value = "";
                dataGridView1.Rows[rowIndex].Cells["seq"].Value = curSeq;
                dataGridView1.Rows[rowIndex].Cells["parentSeq"].Value = 0;
                dataGridView1.Rows[rowIndex].Cells["isDetail"].Value =false;
                PriceBillEntryForm.Rows++;
                parentSeq = curSeq;
               // dr = ((DataTable)dataGridView1.DataSource).NewRow();
               // ((DataTable)dataGridView1.DataSource).Rows.Add(dr);
                dataGridView1.Rows.Add();
                curSeq++;
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["id"].Value = 0; //数据库自动增长不需赋值
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["materialGroupId"].Value = value.FmaterialGroupId;
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["materialId"].Value = value.Fmaterialid;
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["materialGroup"].Value = "";             
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["material"].Value = value.Fmaterial;
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["size"].Value = value.Fsize;
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["unit"].Value = value.Funit;
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["price"].Value = value.Fpric.ToString("0.00");
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["qty"].Value = value.Fqty.ToString("0.0000");
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["amount"].Value = value.Famout.ToString("0.00");
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["description"].Value = value.Fdescription;
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["seq"].Value = curSeq;
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["isDetail"].Value = true;
                dataGridView1.Rows[PriceBillEntryForm.Rows].Cells["parentSeq"].Value = parentSeq;
                dataGridView1.EndEdit();
                PriceBillEntryForm.Rows++;
            }
            else
            {

                parentSeq = Int32.Parse(dataGridView1.Rows[insertIndex].Cells["seq"].Value.ToString());
                curSeq++;
                insertIndex += 1;
                DataGridViewRow row = new DataGridViewRow();
                dataGridView1.Rows.Insert(insertIndex, row);
               // DataRow dr = ((DataTable)dataGridView1.DataSource).NewRow();
               // ((DataTable)dataGridView1.DataSource).Rows.InsertAt(dr,insertIndex);

                dataGridView1.Rows[insertIndex].Cells["materialGroupId"].Value = value.FmaterialGroupId;
                dataGridView1.Rows[insertIndex].Cells["materialId"].Value = value.Fmaterialid;
                dataGridView1.Rows[insertIndex].Cells["material"].Value = value.Fmaterial;
                dataGridView1.Rows[insertIndex].Cells["size"].Value = value.Fsize;
                dataGridView1.Rows[insertIndex].Cells["unit"].Value = value.Funit;
                dataGridView1.Rows[insertIndex].Cells["price"].Value = value.Fpric;
                dataGridView1.Rows[insertIndex].Cells["qty"].Value = value.Fqty;
                dataGridView1.Rows[insertIndex].Cells["amount"].Value = value.Famout;
                dataGridView1.Rows[insertIndex].Cells["description"].Value = value.Fdescription;
                dataGridView1.Rows[insertIndex].Cells["seq"].Value = curSeq;
                dataGridView1.Rows[insertIndex].Cells["isDetail"].Value = true;
                dataGridView1.Rows[insertIndex].Cells["parentSeq"].Value = parentSeq;
                PriceBillEntryForm.Rows++;
            }
              //  this.dataGridView1.RowCount += 1;
            setSumFieldValue();
            
        }
        //单击单元格任意部分时发生
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            RowIndex = e.RowIndex;
        }
        //得到要导出的数据
        public string GetData()
        {
            DataTable table = ExcelNew.SelectFnumberFiceBillEntry(txtNumber.Text);
            string str = "";
            //查询客户的姓名
            string fcustomername = ExcelNew.Selectfidcustomer(int.Parse(table.Rows[0]["fcustomerid"].ToString()));
            str = "" + table.Rows[0]["fname"].ToString() + "计价表!";
            str += "" + table.Rows[0]["fnumber"].ToString() + "|" + fcustomername + "!";
            str += "" + table.Rows[0]["fmodel"].ToString() + "|" + table.Rows[0]["fface"].ToString() + "!";
            str += "" + table.Rows[0]["PriceBill.fsize"].ToString() + "|" + table.Rows[0]["PriceBill.funit"].ToString() + "|" + float.Parse(table.Rows[0]["farea"].ToString()).ToString("0.0000") + "|" + float.Parse(table.Rows[0]["fperimeter"].ToString()).ToString("0.00") + "|" + float.Parse(table.Rows[0]["fdifficulty"].ToString()).ToString("0.00") + "!";
            for (int i = 0; i < table.Rows.Count; i++)
            {
                //MessageBox.Show(table.Rows[i]["fisDetail"].ToString());
                if ((bool)table.Rows[i]["fisDetail"] == false)
                {
                    //查找分类材料名称
                    string fmaterialgroupname = ExcelNew.SelectfidMaterialgroup(int.Parse(table.Rows[i]["fmaterialgroupId"].ToString()));
                    str += "<<" + fmaterialgroupname + ":   " + float.Parse(table.Rows[i]["famount"].ToString()).ToString("0.00") + "  >>!";
                    for (int j = 1; j < table.Rows.Count; j++)
                    {
                        //查找该分类下的材料名称
                        if ((bool)table.Rows[j]["fisDetail"] == true && int.Parse(table.Rows[i]["fseq"].ToString()) == int.Parse(table.Rows[j]["fparentSeq"].ToString()))
                        {
                            string fmaterialname = ExcelNew.SelectfidMaterial(int.Parse(table.Rows[j]["fmaterialid"].ToString()));
                            str += "" + fmaterialname + "| |" + table.Rows[j]["PriceBillEntry.funit"].ToString() + "|" + float.Parse(table.Rows[j]["fprice"].ToString()).ToString("0.00") + "|" + float.Parse(table.Rows[j]["fqty"].ToString()).ToString("0.0000") + "|" + float.Parse(table.Rows[j]["famount"].ToString()).ToString("0.00") + "|" + table.Rows[j]["fdescription"].ToString() + " !";
                        }
                    }
                }
            }
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if ((bool)table.Rows[i]["fisDetail"] == true)
                {
                    str += "管理费用：       " + table.Rows[i]["FmanageFee"].ToString() + "          制造费用：      " + table.Rows[i]["FmadeFee"].ToString() + "         材料费合计：       " + table.Rows[i]["fmaterialFee"].ToString() + "!";
                    str += "成本合计：   " + table.Rows[i]["ftotalCost"].ToString() + "";
                    break;
                }
            }
            //测试输出小数
            //MessageBox.Show(float.Parse(table.Rows[0]["FmadeFee"].ToString()).ToString());
            //float fl = 1.410f;
            //MessageBox.Show(fl.ToString("0.000"));

            //测试通过
            return str;
        }
        //决定难度系数控件只能输入数字和小数
        private void txtDifficulty_KeyPress(object sender, KeyPressEventArgs e)
        {
            string str = this.txtDifficulty.Text;
            e.Handled = e.KeyChar < '0' || e.KeyChar > '9';  //允许输入数字
            if (e.KeyChar == (char)8)  //允许输入回退键
            {
                e.Handled = false;
            }
            if (e.KeyChar == (char)46)
            {
                if (str == "")  //第一个不允许输入小数点
                {
                    e.Handled = true;
                    return;
                }
                else
                { //小数点不允许出现2次
                    foreach (char ch in str)
                    {
                        if (char.IsPunctuation(ch))
                        {
                            e.Handled = true;
                            return;
                        }
                    }
                    e.Handled = false;
                }
            }
        }
        ////传入一个材料分类 和 一个材料名称 计算 表面积
        //public void BasisMaterialarea(string materialname, string materialgroupname)
        //{
        //    string Size = MaterialNew.SelectMaterialgpSize(materialname, materialgroupname);
        //    if (Size != null)
        //    {
        //        string forever = ""; //长
        //        string wide = ""; //宽
        //        string tall = ""; //高
        //        string[] sArray = Size.Split('*');
        //        forever = sArray[0].Trim();
        //        wide = sArray[1].Trim();
        //        tall = sArray[2].Trim();
        //        //float Perimeter;//周长
        //        //float Areaa;//表面积
        //        Area += (float.Parse(forever) * float.Parse(wide) + float.Parse(forever) * float.Parse(tall) + float.Parse(tall) * float.Parse(wide)) * 2;
        //        //Perimeter = (float.Parse(forever) + float.Parse(wide) + float.Parse(tall)) * 4;
        //        txtArea.Text = Area.ToString();
        //        //txtPerimeter.Text = Perimeter.ToString();
        //    }

       //}
        //根据数量 填入表面积面积文本框
        public void BasisMaterialarea(string materialname,string materialgroupname,float fo)
        {
            string Size = MaterialNew.SelectMaterialgpSize(materialname, materialgroupname);
            if (Size != null)
            {
                Area += fo;//累计加表面积
                txtArea.Text = Area.ToString();
            }
        }
        //根据数量  填入周长文本框
        public void BasisMaterialperimeter(string materialname, string materialgroupname, float fo)
        {
            string str = MaterialNew.SelectMaterialgpPerimeter(materialname, materialgroupname);
            if (str != null)
            {
                Perimeter += fo;//累计加表面积
                txtPerimeter.Text = Perimeter.ToString();
            }
        }

        //当控件成功验证后发生 计算管理成本和制造成本
        private void txtDifficulty_Validated(object sender, EventArgs e)
        {
            try
            {
                DataTable table = ParamNew.SelectParam();
                txtManageFee.Text = (float.Parse(table.Rows[0]["fvalue"].ToString()) / float.Parse(txtDifficulty.Text)).ToString("0.00");
                txtMadeFee.Text = (float.Parse(table.Rows[1]["fvalue"].ToString()) / float.Parse(txtDifficulty.Text)).ToString("0.00");
            }
            catch
            {
                //MessageBox.Show("难度系数是必须的","提示");
            }
        }
        //鼠标进入控件时发生
        private void dataGridView1_MouseEnter(object sender, EventArgs e)
        {
            if (txtManageFee.Text.Trim() == "")
            {
                txtManageFee.Text = "0";
            }
            if (txtMadeFee.Text.Trim() == "")
            {
                txtMadeFee.Text = "0";
            }
            if (txtMaterialFee.Text.Trim() == "")
            {
                txtMaterialFee.Text = "0";
            }
            txtTotalCost.Text = (float.Parse(txtManageFee.Text.Trim()) + float.Parse(txtMadeFee.Text.Trim()) + float.Parse(txtMaterialFee.Text.Trim())).ToString();
        }
    }
}
